package com.sims.service.impl;

import java.util.List;

import com.sims.dao.ISimsGradeDao;
import com.sims.dao.impl.SimsGradeDaoImp;
import com.sims.eo.SimsGrade;
import com.sims.service.IGradeService;

public class GradeServiceImp implements IGradeService {
	ISimsGradeDao simsGradeDao = new SimsGradeDaoImp();

	@Override
	public List<SimsGrade> getAllSimsGrade() {
		return simsGradeDao.getAllSimsGrade();
	}

	@Override
	public boolean deletSimsGradeById(Long id) {
		return simsGradeDao.deletSimsGradeById(id);
	}

	@Override
	public SimsGrade getSimsGradeById(Long id) {
		return simsGradeDao.getSimsGradeById(id);
	}

	@Override
	public SimsGrade getSimsGradebyGradeName(String gradeName) {
		return simsGradeDao.getSimsGradebyGradeName(gradeName);
	}

	@Override
	public boolean updateSimsGrade(SimsGrade grade) {
		SimsGrade gradedb = getSimsGradeById(grade.getId());
		int count = (int) simsGradeDao.getRowCountByGradeName(grade
				.getGradeName());
		// 数据库不存在或者不是本条数据
		if (count < 1
				|| (count >= 1 && grade.getGradeName().equals(
						gradedb.getGradeName()))) {
			if (grade.getGradeCode() != null) {
				gradedb.setGradeCode(grade.getGradeCode());
			}
			if (grade.getGradeName() != null) {
				gradedb.setGradeName(grade.getGradeName());
			}
			if (grade.getDepartment() != null) {
				gradedb.setDepartment(grade.getDepartment());
			}
			if (grade.getMajor() != null) {
				gradedb.setMajor(grade.getMajor());
			}
			return simsGradeDao.updateSimsGrade(gradedb);
		}

		return false;
	}

	@Override
	public List<SimsGrade> getAllSimsGrade(int pageNo, int pageSize) {
		return simsGradeDao.getAllSimsGrade(pageNo, pageSize);
	}

	@Override
	public long getRowCount() {
		return simsGradeDao.getRowCount();
	}

	@Override
	public long getRowCountByGradeName(String gradeName) {
		return simsGradeDao.getRowCountByGradeName(gradeName);
	}

	@Override
	public long createGrade(SimsGrade grade) {
		long id = 0;
		int count = (int) simsGradeDao.getRowCountByGradeName(grade
				.getGradeName());
		if (count < 1) {
			return simsGradeDao.createGrade(grade);
		}
		return id;
	}
}
